<template>
  <common-list>
    <!-- 头部搜素start -->
    <common-search slot="search" @search="handleQuery" @reset="resetQuery">
      <el-form
        @submit.native.prevent
        :model="query"
        ref="queryForm"
        :inline="true"
        label-width="65px"
      >
        <el-form-item label="告警状态" prop="status">
          <el-select v-model="query.status" clearable>
            <el-option label="全部" :value="null"></el-option>
            <el-option label="待处理" value="0"></el-option>
            <el-option label="处理中" value="1"></el-option>
            <!-- <el-option label="已处理" value="2"></el-option> -->
            <el-option label="已关闭" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="告警等级" prop="alarmLevel">
          <el-select v-model="query.alarmLevel" clearable>
            <el-option label="全部" :value="null"></el-option>
            <el-option
              v-for="(item, index) in alarmLevel"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="告警时间">
          <el-date-picker
            v-model="date"
            type="datetimerange"
            @change="dateChange"
            value-format="yyyy-MM-dd HH:mm:ss"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['00:00:00', '23:59:59']"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="告警类型" prop="alarmType">
          <el-select v-model="query.alarmType" clearable>
            <el-option label="全部" :value="null"></el-option>
            <el-option
              v-for="(item, index) in alarmType"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="告警码" prop="alarmCode">
          <common-input
            v-model="query.alarmCode"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="源IP" prop="sourceIp">
          <common-input
            v-model="query.sourceIp"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="目标IP" prop="targetIp">
          <common-input
            v-model="query.targetIp"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="系统标识" prop="systemCode">
          <el-select v-model="query.systemCode" clearable>
            <el-option label="全部" :value="null"></el-option>
            <el-option
              v-for="(item, index) in systemnamesList"
              :key="index"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="规则名称" prop="ruleName">
          <common-input
            v-model="query.ruleName"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>
        <common-dept
          ref="commonDept"
          :currentParent="currentParent"
          @getDeptId="getDeptId"
          @getDeptList="getDeptList"
          @getLoading="getLoading"
        ></common-dept>
        <el-form-item label="是否误报" prop="falseAlarmStatus">
          <el-select v-model="query.falseAlarmStatus" clearable>
            <el-option label="全部" :value="null"></el-option>
            <el-option label="是" :value="1" />
            <el-option label="否" :value="0" />
          </el-select>
        </el-form-item>
      </el-form>
    </common-search>

    <!-- 头部搜素end -->

    <!-- 按钮组织start -->
    <el-row slot="tools" :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-download"
          v-hasPermi="['alarmcenter:info:exportandimport']"
          @click="importShow"
          >导入
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-upload2"
          v-hasPermi="['alarmcenter:info:exportandimport']"
          :loading="exportLoading"
          @click="exportData"
          >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          class="claim-btn"
          type="primary"
          plain
          icon="iconfont icon-renling"
          :disabled="!this.selection.length"
          @click="handleClaim"
          >批量确认
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          class="claim-btn"
          type="primary"
          plain
          icon="iconfont icon-renling"
          :disabled="!this.selection.length"
          @click="handleCancel"
          >批量取消
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5" v-hasPermi="['alarmcenter:workorder:handle']">-->
      <!--        <el-dropdown @command="handleCommand">-->
      <!--          <el-button>-->
      <!--            告警处理<i class="el-icon-arrow-down el-icon&#45;&#45;right"></i>-->
      <!--          </el-button>-->
      <!--          <el-dropdown-menu slot="dropdown">-->
      <!--            <el-dropdown-item command="create">工单生成</el-dropdown-item>-->
      <!--            <el-dropdown-item command="handle">人工处理</el-dropdown-item>-->
      <!--          </el-dropdown-menu>-->
      <!--        </el-dropdown>-->
      <!--        &lt;!&ndash; <el-button-->
      <!--          @click="close(1)"-->
      <!--          :disabled="this.selection.length === 0 ? true : false"-->
      <!--        >-->
      <!--          人工处理</el-button-->
      <!--        > &ndash;&gt;-->
      <!--      </el-col>-->
      <el-col :span="1.5" v-hasPermi="['alarmcenter:info:updatestatus']">
        <el-button
          icon="el-icon-close"
          @click="close(3)"
          :disabled="this.selection.length === 0 ? true : false"
          >告警关闭
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          type="primary"
          icon="el-icon-s-check"
          :disabled="this.selection.length === 0 ? true : false"
          @click="HandleDistributeOrg"
          v-hasPermi="['alarmcenter:info:distribution']"
          >分配组织
        </el-button>
      </el-col>
    </el-row>
    <!-- 按钮组织end -->

    <!--列表 start-->
    <common-table
      slot="content"
      v-loading="loading"
      :data="list"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column
        label="系统标识"
        align="left"
        prop="systemCode"
        v-if="isShowColumn('b')"
        show-overflow-tooltip
        width="180"
        fixed
      >
        <template slot-scope="{ row }">{{ row.systemCode }}</template>
      </el-table-column>

      <el-table-column
        label="规则名称"
        align="left"
        prop="ruleName"
        width="180"
        v-if="isShowColumn('g')"
        show-overflow-tooltip
      >
        <template slot-scope="{ row }">{{ row.ruleName }}</template>
      </el-table-column>

      <!-- <el-table-column label="告警子类型" align="left" prop="alarmBusiness"> -->
      <!-- </el-table-column> -->
      <!-- <el-table-column
        label="告警类型"
        align="center"
        prop="alarmType"
        v-if="isShowColumn('d')"
        width="80"
      >
        <template slot-scope="{ row }">
          {{ isAiarmType(row.alarmType) }}
        </template>
      </el-table-column> -->
      <el-table-column
        label="告警码"
        align="left"
        prop="alarmCode"
        width="105"
        show-overflow-tooltip
      ></el-table-column>

      <!--      <el-table-column-->
      <!--        label="风险等级"-->
      <!--        align="center"-->
      <!--        prop="processType"-->
      <!--        width="80"-->
      <!--      >-->
      <!--        <template slot-scope="{ row }">-->
      <!--          <el-tag type="info" size="mini" v-if="row.processType == 0">-->
      <!--            低级-->
      <!--          </el-tag>-->
      <!--          <el-tag type="warning" size="mini" v-if="row.processType == 1">-->
      <!--            中级-->
      <!--          </el-tag>-->
      <!--          <el-tag type="danger" size="mini" v-if="row.processType == 2">-->
      <!--            高级-->
      <!--          </el-tag>-->
      <!--        </template>-->
      <!--      </el-table-column>-->

      <el-table-column
        label="告警级别"
        align="center"
        prop="alarmLevel"
        v-if="isShowColumn('e')"
        width="80"
      >
        <template slot-scope="{ row }">
          <el-tag
            size="mini"
            v-if="row.alarmLevel == 0"
            style="
              color: #409eff;
              background-color: #ecf5ff;
              border-color: #d9ecff;
            "
          >
            {{ row.alarmLevelName }}
          </el-tag>
          <el-tag type="info" size="mini" v-if="row.alarmLevel == 1">
            {{ row.alarmLevelName }}
          </el-tag>
          <el-tag type="warning" size="mini" v-if="row.alarmLevel == 2">
            {{ row.alarmLevelName }}
          </el-tag>
          <el-tag type="danger" size="mini" v-if="row.alarmLevel == 3">
            {{ row.alarmLevelName }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="告警累计次数"
        align="left"
        prop="accumulateTotal"
        show-overflow-tooltip
        width="110px"
      />
      <el-table-column
        show-overflow-tooltip
        label="告警时间"
        align="left"
        prop="alarmTime"
        v-if="isShowColumn('h')"
        width="160"
      >
        <template slot-scope="{ row }"
          >{{ parseTime(new Date(row.alarmTime)) }}
        </template>
      </el-table-column>

      <el-table-column
        label="源IP"
        align="left"
        prop="sourceIp"
        width="160"
        v-if="isShowColumn('f')"
        show-overflow-tooltip
      >
        <template slot-scope="{ row }">
          <Popover :typeValue="row.sourceIp" />
        </template>
      </el-table-column>

      <el-table-column
        label="目标IP"
        align="left"
        prop="targetIp"
        v-if="isShowColumn('i')"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        label="告警描述"
        align="left"
        prop="alarmContent"
        v-if="isShowColumn('j')"
        show-overflow-tooltip
      >
        <template slot-scope="{ row }">{{ row.alarmContent }}</template>
      </el-table-column>
      <el-table-column
        label="状态"
        align="center"
        prop="status"
        v-if="isShowColumn('k')"
        width="120"
      >
        <template slot-scope="{ row }">
          <el-tag type="warning" size="mini" v-if="row.status === 0"
            >待处理
          </el-tag>
          <el-tag size="mini" v-if="row.status === 1">处理中</el-tag>
          <el-tag type="success" size="mini" v-if="row.status === 2"
            >已处理
          </el-tag>
          <el-tag type="danger" size="mini" v-if="row.status === 3"
            >已关闭
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="left" width="160" fixed="right">
        <template slot-scope="{ row }">
          <el-button
            type="text"
            v-if="isReminder(row.workId, row.urgeStatus, row.status)"
            v-hasPermi="['alarmcenter:workorder:speedWork']"
            @click="speedWork(row)"
            >催单
          </el-button>
          <el-button
            type="text"
            v-if="isReminderExt(row.workId, row.urgeStatus)"
            style="color: #7b7b7b"
            >已催单
          </el-button>
          <el-button type="text" @click="showInfo(row)"> 详情 </el-button>
          <el-button
            type="text"
            @click="handleClaim(row)"
            :disabled="row.disabledBtn"
            v-if="row.status === 0"
          >
            确认
          </el-button>
          <el-button
            type="text"
            @click="handleCancel(row)"
            :disabled="row.disabledBtn"
            v-if="row.status === 1"
          >
            取消
          </el-button>
          <el-button
            type="text"
            @click="handleAlarm(row)"
            v-if="row.status !== 3"
            v-hasPermi="['alarmCenter:missRecord:add']"
          >
            误报处理
          </el-button>
        </template>
      </el-table-column>
    </common-table>

    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="query.pageNum"
      :limit.sync="query.pageSize"
      @pagination="getList"
    />
    <!-- 列表 end -->
    <editColumnDialog
      ref="editColumnDialog"
      @checked="(checked) => (columnShowList = checked)"
    />
    <createDialog ref="createDialog" @refresh="getList" />
    <handleDialog ref="handleDialog" @refresh="getList" />

    <alarm-handle ref="alarmHandle" @refresh="getList" />

    <common-upload-dialog
      download-path="system/alarmInfo/downFileByemplateName"
      upload-path="/system/alarmInfo/importData"
      template-name="alarmTemplate.xls"
      report-path="system/downErrorReportByemplateName"
      ref="uploadDialog"
      :getList="getList"
    />

    <common-info-dialog :visible="isAlarmInfo" @cancel="isAlarmInfo = false">
      <common-group-title
        title="预警信息"
        style="padding: 0 20px"
      ></common-group-title>
      <common-info-table>
        <common-info-table-cell label="事件名称">
          {{ info.alarmBusiness }}
        </common-info-table-cell>
        <common-info-table-cell label="事件类型">
          {{ isAiarmType(info.alarmType) }}
        </common-info-table-cell>
        <common-info-table-cell label="发生时间">
          {{ parseTime(new Date(info.alarmTime)) }}
        </common-info-table-cell>
        <common-info-table-cell label="触发者IP">
          {{ info.sourceIp }}
        </common-info-table-cell>
        <common-info-table-cell label="预警等级">
          {{ info.alarmLevelName }}级
        </common-info-table-cell>
      </common-info-table>

      <common-group-title
        title="风险信息"
        style="padding: 0 20px"
      ></common-group-title>
      <common-info-table>
        <common-info-table-cell label="风险名称">
          {{ alarmInfo.dangerName }}
        </common-info-table-cell>
        <common-info-table-cell label="风险等级">
          {{
            info.processType == 0
              ? "低级"
              : info.processType == 1
              ? "中级"
              : info.processType == 2
              ? "高级"
              : ""
          }}
        </common-info-table-cell>
        <common-info-table-cell label="事件概述">
          {{ alarmInfo.alarmContent }}
        </common-info-table-cell>
        <common-info-table-cell label="风险成因">
          {{ alarmInfo.alarmCause }}
        </common-info-table-cell>
        <common-info-table-cell label="风险描述">
          {{ alarmInfo.dangerContent }}
        </common-info-table-cell>
      </common-info-table>

      <common-group-title
        title="事件详情"
        style="padding: 0 20px"
      ></common-group-title>
      <common-info-table>
        <common-info-table-cell label="访问者账号">
          {{ alarmInfo.visitAccount }}
        </common-info-table-cell>
        <common-info-table-cell label="访问者时间">
          {{ alarmInfo.visitTime }}
        </common-info-table-cell>
        <common-info-table-cell label="源MAC信息">
          {{ alarmInfo.mac }}
        </common-info-table-cell>
        <common-info-table-cell label="应用域名">
          {{ alarmInfo.address }}
        </common-info-table-cell>
        <common-info-table-cell label="应用服务IP">
          {{ alarmInfo.domainIp }}
        </common-info-table-cell>
        <common-info-table-cell label="敏感信息标签">
          {{ alarmInfo.sensitiveNameList }}
        </common-info-table-cell>
        <common-info-table-cell label="敏感信息详情">
          {{ alarmInfo.sensitiveList }}
        </common-info-table-cell>
        <common-info-table-cell label="请求头信息">
          {{ alarmInfo.requestHead }}
        </common-info-table-cell>
        <common-info-table-cell label="请求内容">
          <div style="max-height: 500px; overflow: scroll">
            <code-editor
              v-if="isAlarmInfo"
              key="requestBody"
              :value="alarmInfo.requestBody"
              :code="alarmInfo.requestCode"
              :read-only="true"
              :border="false"
            ></code-editor>
          </div>
        </common-info-table-cell>

        <common-info-table-cell label="响应头信息">
          {{ alarmInfo.responseHead }}
        </common-info-table-cell>
        <common-info-table-cell label="响应内容">
          <div style="max-height: 500px; overflow: scroll">
            <code-editor
              v-if="isAlarmInfo"
              key="responseBody"
              :value="alarmInfo.responseBody"
              :read-only="true"
              :code="alarmInfo.responseCode"
              :border="false"
            ></code-editor>
          </div>
        </common-info-table-cell>
      </common-info-table>
    </common-info-dialog>

    <distribute-org
      ref="distributeOrg"
      :deptSelectArr="deptSelectArr"
      :dialogShow="dialogShow"
      :buttonLoading="submitOrgLoading"
      @toSubmit="toDistributeOrg"
      @close="dialogShow = false"
    />
    <template v-if="isalarmInfo">
      <alarm-info
        ref="alarmInfo"
        @close="alarmInfo"
        :targetIp="targetIp"
        :detail="detailForm"
      ></alarm-info>
    </template>
  </common-list>
</template>

<script>
import editColumnDialog from "./components/editColumnDialog";
import createDialog from "./components/createDialog.vue";
import handleDialog from "./components/handleDialog.vue";
import AlarmInfo from "./components/alarmInfo.vue";
import alarmHandle from "./components/alarmHandle.vue";
import {
  alarmInfo_list,
  alarmInfo_alarmType,
  alarmInfo_updateAlarmStatus,
  alarmInfo_alarmLevel,
  workOrder_speedWork,
  systemnames,
  queryByLoginToken,
  updateDeptId,
  getAssetInfo,
} from "@/api/alarmCenter/index";
import BaseList from "@/mixins/BaseList";
import CodeEditor from "@/components/CodeEditor";
import { getSingleArray, getFullDept } from "@/utils/util";
import DistributeOrg from "../apisecurity/application/components/distributeOrg.vue";
import Popover from "./components/Popover.vue";

export default {
  components: {
    editColumnDialog,
    createDialog,
    handleDialog,
    AlarmInfo,
    CodeEditor,
    DistributeOrg,
    alarmHandle,
    Popover,
  },
  props: {},
  mixins: [BaseList],
  data() {
    return {
      exportLoading: false,
      dialogShow: false,
      submitOrgLoading: false,
      targetIp: {},
      isalarmInfo: false,
      date: [],
      total: 1, // 总条数
      loading: false, // table 加载中
      list: [], // 数据列
      selectionIds: "", // 列表选中数据id
      selection: [],
      single: true, // 非单个禁用
      multiple: true, // 非多个禁用
      query: {
        accumulateTotal: null,
        alarmContent: null,
        systemCode: null,
        pageNum: 1,
        pageSize: 10,
        sourceIp: "",
        ruleName: "",
        targetIp: "",
        alarmTime: {},
        alarmType: null,
        alarmLevel: null,
        status: "0",
        deptIds: null,
        alarmCode: null,
        falseAlarmStatus: null,
      }, // 查询条件
      columnShowList: [],
      detailView: false, // 详情页面显示
      alarmType: [],
      alarmLevel: [],
      systemnamesList: [],
      isAlarmInfo: false,
      info: {},
      singleDeptList: [],
      currentParent: 2,
      deptSelectArr: [],
      lastSourceIp: "",
      lastResults: {},
    };
  },
  watch: {},
  computed: {},
  methods: {
    handleClaim(row) {
      if (
        this.selection.length &&
        this.selection.filter((item) => item.status !== 0).length
      )
        return this.$GlobalErrorConfirm(
          "操作失败",
          "当前所选告警存在处理中状态或者关闭状态，无法确认！"
        );
      let ids = row?.id || this.selectionIds.join(",");
      row.disabledBtn = true;
      alarmInfo_updateAlarmStatus({
        alarmIds: ids,
        status: 1,
      })
        .then((res) => {
          if (res && res.code === 200) {
            this.$GlobalSuccessMessage("操作成功");
            this.getList();
          } else {
            this.$GlobalErrorMessage("操作失败", res.msg);
          }
        })
        .finally(() => {
          row.disabledBtn = false;
        });
    },

    handleCancel(row) {
      if (
        this.selection.length &&
        this.selection.filter((item) => item.status !== 1).length
      )
        return this.$GlobalErrorConfirm(
          "操作失败",
          "当前所选告警存在待处理状态或者关闭状态，无法取消确认！"
        );
      let ids = row?.id || this.selectionIds.join(",");
      row.disabledBtn = true;
      alarmInfo_updateAlarmStatus({
        alarmIds: ids,
        status: 0,
      })
        .then((res) => {
          if (res && res.code === 200) {
            this.$GlobalSuccessMessage("操作成功");
            this.getList();
          } else {
            this.$GlobalErrorMessage("操作失败", res.msg);
          }
        })
        .finally(() => {
          row.disabledBtn = false;
        });
    },
    assetInfoShow(row) {
      row.assetInfoVisible = true;
    },
    assetInfoHidden(row) {
      row.assetInfoVisible = false;
    },

    dateChange() {
      let endTime = null,
        beginTime = null;
      if (this.date && this.date.length > 0) {
        beginTime = this.date[0];
        endTime = this.date[1];
      }
      this.query.beginTime = beginTime || null;
      this.query.endTime = endTime || null;
    },

    getLoading() {
      this.loading = true;
    },
    getDeptList(list) {
      this.deptSelectArr = list;
      this.singleDeptList = getSingleArray(list);
      this.getList();
    },
    getDeptId(val) {
      this.query.deptIds = val;
    },
    getDeptName(val) {
      let dept = this.singleDeptList.find((item) => item.id === val.toString());
      if (dept) return getFullDept(dept.fulllabel);
    },

    //误报处理
    handleAlarm(row) {
      this.$refs.alarmHandle.show(row ? { ...row } : {});
    },

    showInfo(row) {
      this.targetIp = { targetIp: row.targetIp, sourceIp: row.sourceIp };
      this.isalarmInfo = true;
      row.alarmType = this.isAiarmType(row.alarmType);
      row.deptName = this.getDeptName(row.deptId || "");
      this.detailForm = row;
    },
    alarmInfo() {
      this.isalarmInfo = false;
    },
    systemnames() {
      systemnames().then((res) => {
        if (res && res.code === 200) {
          this.systemnamesList = res.data;
        } else {
          this.systemnamesList = [];
        }
      });
    },
    speedWork(row) {
      workOrder_speedWork({
        alarmId: row.id,
        workId: row.workId,
      })
        .then((res) => {
          if (res && res.code === 200) {
            this.$GlobalSuccessMessage("催单成功");
            this.getList();
          } else {
            this.$GlobalErrorMessage(res.msg);
          }
        })
        .catch((err) => {
          this.$GlobalErrorMessage(err.msg);
        });
    },
    handleCommand(type) {
      let selectionIds = this.selectionIds;
      if (selectionIds && selectionIds.length == 1) {
        let canDo = true,
          msg = "",
          msg1 = "";
        let canCreate = true;
        this.selection.map((v) => {
          if (v.status == 3) {
            canDo = false;
            msg = "[已关闭] 的告警不可重复处理";
          }
          if (v.status == 1) {
            canDo = false;
            msg = "[处理中] 的告警不可重复处理";
          }
          // if (v.systemCode === 'bwda_inspection') {
          if (v.systemCodeKey !== "0X0003") {
            canCreate = false;
            msg1 = "不支持人工处理";
          }
        });

        if (type == "handle") {
          if (!canCreate) {
            this.$GlobalErrorConfirm("操作失败", msg1);
            return;
          }
          this.$refs.handleDialog.show(selectionIds.join(","));
        } else {
          if (!canDo) {
            this.$GlobalErrorConfirm("操作失败", msg);
            return;
          }
          this.$refs.createDialog.show(selectionIds.join(","));
        }
      }
      if (selectionIds && selectionIds.length > 0) {
        let canDos = true,
          msg = "",
          msg1 = "";
        let canCreates = true;
        this.selection.map((v) => {
          if (v.status != 0) {
            canDos = false;
            msg = "仅支持处理状态为 [待处理] 的告警";
          }
          // if (v.systemCode === 'bwda_inspection') {

          if (v.systemCodeKey !== "0X0003") {
            canCreates = false;
            msg1 = "不支持人工处理";
          }
        });

        if (type == "handle") {
          if (!canCreates) {
            this.$GlobalErrorConfirm("操作失败", msg1);
            return;
          }
          this.$refs.handleDialog.show(selectionIds.join(","));
        } else {
          if (!canDos) {
            this.$GlobalErrorConfirm("操作失败", msg);
            return;
          }
          this.$refs.createDialog.show(selectionIds.join(","));
        }
      } else {
        this.$GlobalErrorConfirm("操作失败", "请选择 [待处理] 的告警");
      }
    },
    close(row) {
      if (row && row === 1) {
        let selection = this.selection;

        if (selection.length > 0) {
          let arr = selection.find((item) => item.status == 3);
          if (arr) {
            this.$GlobalErrorConfirm("操作失败", "已关闭的告警无法再次关闭");
            return false;
          }
          let d = this.selectionIds.join(",");
          this.$GlobalDeleteConfirm("是否进行人工处理")
            .then(() => {
              alarmInfo_updateAlarmStatus({
                alarmIds: d,
                status: row,
              })
                .then((res) => {
                  if (res && res.code === 200) {
                    this.$GlobalSuccessMessage("操作成功");
                    this.getList();
                  } else {
                    this.$GlobalErrorMessage(res.msg);
                  }
                })
                .catch((err) => {
                  this.$GlobalErrorMessage(err.msg);
                });
            })
            .catch(() => {});
        } else {
          this.$GlobalErrorConfirm("操作失败", "请选择要处理的告警");
        }
      } else {
        let selection = this.selection;
        if (selection.length > 0) {
          let arr = selection.find((item) => item.status == 3);
          if (arr) {
            this.$GlobalErrorConfirm("操作失败", "已关闭的告警无法再次关闭");
            return false;
          }
          let d = this.selectionIds.join(",");
          this.$GlobalDeleteConfirm("是否进行告警关闭")
            .then(() => {
              alarmInfo_updateAlarmStatus({
                alarmIds: d,
                status: row,
              })
                .then((res) => {
                  if (res && res.code === 200) {
                    this.$GlobalSuccessMessage("关闭成功");
                    this.getList();
                  } else {
                    this.$GlobalErrorMessage(res.msg);
                  }
                })
                .catch((err) => {
                  this.$GlobalErrorMessage(err.msg);
                });
            })
            .catch(() => {});
        } else {
          this.$GlobalErrorConfirm("操作失败", "请选择要关闭的告警");
        }
      }
    },
    exportData() {
      queryByLoginToken().then((res) => {
        if (res && res.code === 200) {
          this.$GlobalDeleteConfirm(
            "最多支持导出" + res.data + "条数据,确定导出吗?"
          )
            .then(() => {
              this.exportLoading = true;
              this.download(
                "system/alarmInfo/export",
                {
                  ...this.query,
                  pageSize: res.data,
                },
                `告警中心_${new Date().getTime()}.xlsx`
              ).finally(() => {
                this.exportLoading = false;
              });
            })
            .catch(() => {});
        }
      });
    },
    importShow() {
      this.$refs.uploadDialog.show();
    },
    isAiarmType(t) {
      let r = this.alarmType.find((item) => item.id == t);
      return r ? r.name : "-";
    },
    /**
     * 是否显示列
     */
    isShowColumn(key) {
      let columnShowList = this.columnShowList;
      if (columnShowList.length > 0) {
        return columnShowList.indexOf(key) > -1;
      } else {
        return true;
      }
    },
    /**
     * 是否催单
     */
    isReminder(workid, urgeStatus, status) {
      if (workid !== null && workid !== undefined && workid !== "") {
        if (urgeStatus == 0) {
          if (status == 3) {
            return false;
          }
          return true;
        }
      }
      return false;
    },
    /**
     * 是否催单
     */
    isReminderExt(workid, urgeStatus) {
      if (workid !== null && workid !== undefined && workid !== "") {
        if (urgeStatus == 1) {
          return true;
        }
      }
      return false;
    },
    /**
     * 选择
     */
    handleSelectionChange(selection) {
      if (selection && selection.length !== 0) {
        this.batch = true;
      } else {
        this.batch = false;
      }
      this.selectionIds = selection.map((item) => item.id);
      this.selection = selection;
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /**
     * 获取列表
     */
    getList() {
      this.loading = true;
      let query = { ...this.query };
      alarmInfo_list(query)
        .then((res) => {
          this.loading = false;
          if (res && res.code === 200) {
            this.list = res.rows.reduce((r, c) => {
              r.push({
                ...c,
                disabledBtn: false,
              });
              return r;
            }, []);

            this.total = res.total;
          } else {
            this.list = [];
            this.total = 0;
            this.handleListTips(res);
          }
        })
        .catch((err) => {
          this.list = [];
          this.total = 0;
          this.handleListTips(err);
        })
        .finally(() => {
          this.selection = [];
        });
    },
    /**
     * 查询
     */
    handleQuery() {
      this.query.pageNum = 1;
      this.getList();
    },
    /**
     * 重置查询
     */
    resetQuery() {
      this.query = this.$options.data().query;
      this.date = [];
      this.$refs.commonDept.clearDept();
    },
    /**
     * 添加/编辑
     */
    editColumn() {
      this.$refs.editColumnDialog.show(this.columnShowList);
    },
    getAlarmType() {
      alarmInfo_alarmType()
        .then((res) => {
          if (res && res.code === 200) {
            this.alarmType = res.data;
          } else {
            this.alarmType = [];
          }
        })
        .catch((err) => {
          this.alarmType = [];
        });
      alarmInfo_alarmLevel()
        .then((res) => {
          if (res && res.code === 200) {
            this.alarmLevel = res.data;
          } else {
            this.alarmLevel = [];
          }
        })
        .catch((err) => {
          this.alarmLevel = [];
        });
    },

    HandleDistributeOrg() {
      // let includeDistributeOrg = this.list.find((v) => {
      //   return this.selectionIds.includes(v.id) && v.deptId;
      // });
      // if (includeDistributeOrg) {
      //   this.$GlobalErrorConfirm("分配失败", "包含已分配组织的数据");
      //   return;
      // }
      this.dialogShow = true;
      this.$refs.distributeOrg.show();
    },

    toDistributeOrg(val) {
      // console.log(this.selectionIds);
      if (val) {
        this.submitOrgLoading = true;
        updateDeptId({ ids: this.selectionIds, deptId: val })
          .then((res) => {
            if (res && res.code === 200) {
              this.$GlobalSuccessMessage("分配成功");
              this.dialogShow = false;
              this.getList();
            } else {
              this.$GlobalErrorConfirm("分配失败", res.msg);
            }
          })
          .catch((err) => {
            this.$GlobalErrorConfirm("分配失败", err.msg);
          })
          .finally(() => {
            this.submitOrgLoading = false;
          });
      } else {
        this.$GlobalErrorConfirm("分配失败", "请选择组织");
      }
    },

    setDisabledOption(data) {
      if (data.length > 0) {
        data.forEach((f) => {
          f.disabled = f.freeze !== "N";
          if (f.children && f.children.length > 0) {
            this.setDisabledOption(f.children);
          }
        });
      }
    },
  },
  created() {
    // this.getList();
    this.getAlarmType();
    this.systemnames();
  },
  mounted() {},
};
</script>
<style lang="scss" scoped>
::v-deep .common-input-number {
  width: 100%;
}

.link {
  color: var(--theme-color);
  cursor: pointer;
}

::v-deep .claim-btn {
  .iconfont {
    font-size: 12px;
  }
  span {
    margin-left: 5px;
  }
}
</style>
